package br.com.klis.batendoumabola.server;

import java.util.ArrayList;
import java.util.Iterator;

import br.com.klis.batendoumabola.client.service.UserImageService;
import br.com.klis.batendoumabola.shared.Imagem;

import com.google.appengine.api.blobstore.BlobInfo;
import com.google.appengine.api.blobstore.BlobInfoFactory;
import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.blobstore.BlobstoreService;
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;
import com.google.appengine.api.images.ImagesService;
import com.google.appengine.api.images.ImagesServiceFactory;
import com.google.appengine.api.utils.SystemProperty;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

@SuppressWarnings("serial")
public class UserImageServiceImpl extends RemoteServiceServlet implements
		UserImageService {
	private static final boolean PRODUCTION_MODE = SystemProperty.environment.value() == SystemProperty.Environment.Value.Production;          

	@Override
	public String getBlobstoreUploadUrl() {
		System.out.println("USERIMAGESERVICEIMPL - getblobstoreuploadurl: Chegou aqui...");
		BlobstoreService blobstoreService = BlobstoreServiceFactory
				.getBlobstoreService();
		String retorno = blobstoreService.createUploadUrl("/upload");
		System.out.println("USERIMAGESERVICEIMPL - getblobstoreuploadurl - retorno: " + retorno);
		return retorno;
	}
	
	public ArrayList<Imagem> getImagens() {
		ImagesService imagesService = ImagesServiceFactory.getImagesService();

		
        //final List<BlobKey> userBlobKeys = getUserBlobKeys();
          final BlobInfoFactory blobInfoFactory = new BlobInfoFactory();
        final Iterator<BlobInfo> blobInfos = blobInfoFactory.queryBlobInfos();
        final ArrayList<Imagem> imagens = new ArrayList<Imagem>();
        
          while (blobInfos.hasNext()) {
            final BlobInfo blobInfo = blobInfos.next();
            //if (userBlobKeys.contains(blobInfo.getBlobKey())) {
                final Imagem imagem = new Imagem();
                imagem.setBlobKey(blobInfo.getBlobKey().getKeyString());
                imagem.setFileName(blobInfo.getFilename());
                imagem.setContentType(blobInfo.getContentType());
                imagem.setCreation(blobInfo.getCreation());
                imagem.setSize(blobInfo.getSize());
                imagem.setImageUrl(getServingUrl(blobInfo.getBlobKey()));
                System.out.println("SERVER: "+imagesService.getServingUrl(blobInfo.getBlobKey()));
                imagens.add(imagem);
            }
        //}
        return imagens;
    }
	
	private String getServingUrl(BlobKey blobKey) {
		String servingUrl = ImagesServiceFactory.getImagesService().getServingUrl(blobKey,32,true);
		// Hack for Dev mode
		if (PRODUCTION_MODE) {
			return servingUrl;
		} else {
			return servingUrl.replaceFirst("http://0.0.0.0:8888", "");
		}
	}

	/*@Override
	public UploadedImage get(String key) {
		UploadedImageDao dao = new UploadedImageDao();
		UploadedImage image = dao.get(key);
		return image;
	}

	@Override
	public List<UploadedImage> getRecentlyUploaded() {
		UploadedImageDao dao = new UploadedImageDao();
		List<UploadedImage> images = dao.getRecent(); 
		return images;
	}

	@Override
	public void deleteImage(String key) {
		UserService userService = UserServiceFactory.getUserService();
		User user = userService.getCurrentUser();
		UploadedImageDao dao = new UploadedImageDao();
		UploadedImage image = dao.get(key);
		if(image.getOwnerId().equals(user.getUserId())) {
			dao.delete(key);
		}
	}

	public String tagImage(Tag tag) {
		UserService userService = UserServiceFactory.getUserService();
		User user = userService.getCurrentUser();
		TagDao dao = new TagDao();

		// TODO: Do validation here of x, y, ImageId
		
		tag.setTaggerId(user.getUserId());
		tag.setCreatedAt(new Date());
		
		String key = dao.put(tag);
		return key;
	}

	@Override
	public List<Tag> getTagsForImage(UploadedImage image) {
		TagDao dao = new TagDao();
		List<Tag> tags = dao.getForImage(image);
		return tags;
	}
*/
}
